package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.RoleUserMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Override
    public PageResponse page(Integer pageNum, Integer pageSize, RoleDto roleDto) {
        if(pageNum <= 0 ){
            pageNum = 1;
        }
        if(pageSize <= 0){
            pageSize = 5;
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<RoleVo> page = roleMapper.page(roleDto.getRoleName());
        PageResponse<RoleVo> of = PageResponse.of(page, RoleVo.class);
        return of;
    }

    @Override
    public void insert(RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        roleMapper.insert(role);
    }

    @Override
    public List<String> findCheckedResources(Long roleId) {
        List<String> list = roleMapper.findCheckedResources(roleId);
        return list;
    }

    @Override
    public Boolean update(RoleDto roleDto) {
        Role role = roleMapper.selectById(roleDto.getId());
        if(ObjectUtil.isNotNull(roleDto.getDataState()) && !role.getDataState().equals(roleDto.getDataState()) && roleDto.getDataState().equals("1")){
            Integer count = roleUserMapper.countUser(roleDto.getId());
            if(count > 0){
                throw new BaseException(BasicEnum.ROLE_DISTRIBUTED_FORBIDDEN);
            }
            role.setDataState(roleDto.getDataState());
        }else if(ObjectUtil.isNotNull(roleDto.getDataState())){
            role.setDataState(roleDto.getDataState());
        }
        if(ObjectUtil.isNotNull(roleDto.getRoleName())){
            role.setRoleName(roleDto.getRoleName());
        }
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);

        if(ObjectUtil.isNull(roleDto.getCheckedResourceNos())){
            return true;
        }
        roleResourceMapper.deleteById(roleDto.getId());
        List<RoleResource> collect = Arrays.asList(roleDto.getCheckedResourceNos()).stream().map(r -> {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(role.getId());
            roleResource.setResourceNo(r);
            roleResource.setDataState(SuperConstant.DATA_STATE_0);
            roleResource.setCreateBy(1671403256519078138L);
            return roleResource;
        }).collect(Collectors.toList());

        roleResourceMapper.batchInsert(collect);

        return true;
    }

    @Override
    public void delete(Long roleId) {
        Role role = roleMapper.selectById(roleId);
        if(role.getDataState().equals("0")){
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        roleResourceMapper.deleteById(roleId);
        roleMapper.deleteById(roleId);
    }

    @Override
    public List<RoleVo> getAllRoel() {
        List<RoleVo> list = roleMapper.getAllRole();
        return list;
    }
}
